﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;


namespace TInventory
{
    public partial class AddEditInventory : Form
    {
        TIEntities dbContext;
        List<ProductType> productTypeList;
        List<ProductSubType> productSubTypeList;

        public DataGridViewRow selectedRecord = null;

        public AddEditInventory()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            FillTypeCombo();
            FillSupplierCombo();
        }

        private void FillTypeCombo()
        {
            productTypeList = (from pt in dbContext.ProductTypes select pt).Distinct().ToList();
            if (productTypeList != null)
            {
                BindDataWithTypeCombo(productTypeList);
            }
            else
            {
                productTypeList = new List<ProductType>();
            }

        }

        private void BindDataWithTypeCombo(List<ProductType> productTypeList)
        {

            this.comboBoxProductType.DataSource = null;
            comboBoxProductType.DisplayMember = "Name";
            comboBoxProductType.ValueMember = "ID";
            comboBoxProductType.DataSource = productTypeList;
            comboBoxProductType.Refresh();
        }


        private void FillSupplierCombo()
        {

            List<Supplier> supplierList = (from pt in dbContext.Suppliers select pt).ToList<Supplier>();
            if (supplierList != null)
            {
                BindDataWithSupplierCombo(supplierList);
            }
            else
            {
                productTypeList = new List<ProductType>();
            }

        }

        private void BindDataWithSupplierCombo(List<Supplier> supplierList)
        {

            this.comboBoxSupplier.DataSource = null;
            comboBoxSupplier.DisplayMember = "ContactPersonName";
            comboBoxSupplier.ValueMember = "ID";
            comboBoxSupplier.DataSource = supplierList;
            comboBoxSupplier.Refresh();
        }




        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (selectedRecord == null) // Add New Record
            {
                try
                {
                    Inventory inv = new Inventory();

                    inv.ItemCode=this.textBoxItemCode.Text;
                    inv.ItemName=this.textBoxItemName.Text;
                    inv.Description=this.textBoxDescription.Text;
                    int quantity = int.Parse(this.textBoxQuantity.Text);
                    int price = int.Parse(this.textPrice.Text);

                    if(this.radioButtonTotalPrice.Checked == true)
                    {
                        inv.Price=price / quantity;
                    }
                    else
                    {
                        inv.Price = price;
                    }

                    inv.Quantity=quantity;
                    inv.PurchaseDate=this.dateTimePickerPurchaseDate.Value;
                    inv.SupplierID=int.Parse(this.comboBoxSupplier.SelectedValue.ToString());
                    inv.ProductTypeID=int.Parse(this.comboBoxProductType.SelectedValue.ToString());
                    try
                    {
                        inv.ProductSubTypeID = int.Parse(this.comboBoxProductSubType.SelectedValue.ToString());
                    }
                    catch { }
                    try
                    {
                        inv.SizeID = int.Parse(this.comboBoxSize.SelectedValue.ToString());
                    }
                    catch { }



                    dbContext.AddToInventories(inv);
                    dbContext.SaveChanges();

                    MessageBox.Show("New Record Inserted");


                    this.textBoxItemCode.Text = "";
                    this.textBoxItemName.Text = "";
                    this.textBoxDescription.Text = "";
                    this.textBoxQuantity.Text = "1";
                    this.textPrice.Text = "1";
                    this.dateTimePickerPurchaseDate.Value = DateTime.Today;
                    this.comboBoxSupplier.SelectedIndex=0;
                    this.comboBoxProductType.SelectedIndex=0;
                    try
                    {
                        this.comboBoxProductSubType.SelectedIndex = 0;
                    }
                    catch { }

                    try
                    {
                        this.comboBoxSize.SelectedIndex = 0;
                    }
                    catch { }

                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Some Error Occured");
                }

            }
            else
            {
                try
                {

                    int id = int.Parse(selectedRecord.Cells["ID"].Value.ToString());
                    Inventory inv = (from s in dbContext.Inventories where s.ID == id select s).FirstOrDefault();

                    inv.ItemCode = this.textBoxItemCode.Text;
                    inv.ItemName = this.textBoxItemName.Text;
                    inv.Description = this.textBoxDescription.Text;
                    int quantity = int.Parse(this.textBoxQuantity.Text);
                    int price = int.Parse(this.textPrice.Text);

                    if (this.radioButtonTotalPrice.Checked == true)
                    {
                        inv.Price = price / quantity;
                    }
                    else
                    {
                        inv.Price = price;
                    }

                    inv.Quantity = quantity;
                    inv.PurchaseDate = this.dateTimePickerPurchaseDate.Value;
                    inv.SupplierID = int.Parse(this.comboBoxSupplier.SelectedValue.ToString());
                    inv.ProductTypeID = int.Parse(this.comboBoxProductType.SelectedValue.ToString());
                    try
                    {
                        inv.ProductSubTypeID = int.Parse(this.comboBoxProductSubType.SelectedValue.ToString());
                    }
                    catch { }
                    try
                    {
                        inv.SizeID = int.Parse(this.comboBoxSize.SelectedValue.ToString());
                    }
                    catch { }


                    dbContext.SaveChanges();
                }
                catch {
                    MessageBox.Show("Some Error Occured.");
                }

                this.Close();

            }
        }

        private void AddEditInventory_Load(object sender, EventArgs e)
        {
            if (this.comboBoxSupplier.SelectedIndex == -1)
            {
                MessageBox.Show("Supplier does not exist, please select the correct Supplier");
                return;
            }

            

            this.dateTimePickerPurchaseDate.Value = DateTime.Now;
            try
            {
                this.textBoxItemCode.Text = selectedRecord.Cells["ItemCode"].Value.ToString();
            }
            catch { }
            try
            {
            this.textBoxItemName.Text = selectedRecord.Cells["ItemName"].Value.ToString();
            }
            catch { }
            try
            {
            this.textPrice.Text = selectedRecord.Cells["Price"].Value.ToString();
             }
            catch { }
             try
            {
            this.textBoxQuantity.Text = selectedRecord.Cells["Quantity"].Value.ToString();
            }
            catch { }
            try
            {
                this.dateTimePickerPurchaseDate.Value =  DateTime.Parse(selectedRecord.Cells["PurchaseDate"].Value.ToString()); 
            }
            catch { }
            try
            {
            this.textBoxDescription.Text = selectedRecord.Cells["Description"].Value.ToString();
            }
            catch { }
            try
            {
                this.comboBoxSupplier.SelectedValue = int.Parse(selectedRecord.Cells["SupplierID"].Value.ToString());
            }
            catch { }
            try
            {
                this.comboBoxProductType.SelectedValue = int.Parse(selectedRecord.Cells["ProductTypeID"].Value.ToString());
            }
            catch { }
            try
            {
                this.comboBoxProductSubType.SelectedValue = int.Parse(selectedRecord.Cells["ProductSubTypeID"].Value.ToString());
            }
            catch { }
            try
            {
                this.comboBoxSize.SelectedValue = int.Parse(selectedRecord.Cells["SizeID"].Value.ToString());
            }
            catch { }
            
                    
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxProductType_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillSubTypeCombo();
            FillSizeCombo();
        }

        private void FillSubTypeCombo()
        {
            int selectedTypeId = Convert.ToInt32(this.comboBoxProductType.SelectedValue.ToString());
            List<ProductSubType> productSubTypeList = (from pt in dbContext.ProductSubTypes where pt.ProductTypeID == selectedTypeId select pt).ToList<ProductSubType>();
            if (productSubTypeList != null)
            {
                BindDataWithSubTypeCombo(productSubTypeList);
            }
            else
            {
                productSubTypeList = new List<ProductSubType>();
            }
        }

        private void BindDataWithSubTypeCombo(List<ProductSubType> productSubTypeList)
        {

            this.comboBoxProductSubType.DataSource = null;
            comboBoxProductSubType.DisplayMember = "Name";
            comboBoxProductSubType.ValueMember = "ID";
            comboBoxProductSubType.DataSource = productSubTypeList;
            comboBoxProductSubType.Refresh();
        }

        private void comboBoxProductSubType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        private void FillSizeCombo()
        {
            int selectedTypeId = Convert.ToInt32(this.comboBoxProductType.SelectedValue.ToString());
            List<ProductSize> productSizeList = (from pt in dbContext.ProductSizes where pt.TypeID == selectedTypeId select pt).ToList<ProductSize>();
            if (productSizeList != null)
            {
                BindDataWithSizeCombo(productSizeList);
            }
            else
            {
                productSizeList = new List<ProductSize>();
            }
        }

        private void BindDataWithSizeCombo(List<ProductSize> productSizeList)
        {

            this.comboBoxSize.DataSource = null;
            comboBoxSize.DisplayMember = "Name";
            comboBoxSize.ValueMember = "ID";
            comboBoxSize.DataSource = productSizeList;
            comboBoxSize.Refresh();
        }

    }
}
